#pragma region Licensing
/*
       LOGL::GLib -> LorgonJortle's OpenGL Game Library
       Copyright (C) 2011  Jesse 'LorgonJortle' Wilkerson

       This program is free software: you can redistribute it and/or modify
       it under the terms of the GNU General Public License as published by
       the Free Software Foundation, either version 3 of the License, or
       (at your option) any later version.

       This program is distributed in the hope that it will be useful,
       but WITHOUT ANY WARRANTY; without even the implied warranty of
       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       GNU General Public License for more details.

       You should have received a copy of the GNU General Public License
       along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#pragma endregion

#include "Cube.h"

namespace LOGL_GLib
{
     namespace Common
     {
          /******************************************************************************
           Cube::Cube(GLfloat _Size);
                    Arguments:
                         GLfloat _Size:
                              The dimensions of the Cube.

                    Description:
                         Construct a Cube with the specified size.

                    Returns:
                         The Cube object.
          *******************************************************************************/
          Cube::Cube(GLfloat _Size)
          {
               /* Create our array of vertices. */
               Vector3 verts[24];

               /* Cut size in half so we can just mirror it. */
               GLfloat size = _Size / 2.0f;

               /* Front face. */
               verts[0].X = -size; verts[0].Y = size; verts[0].Z = 0.0f;
               verts[1].X = -size; verts[1].Y = -size; verts[1].Z = 0.0f;
               verts[2].X = size; verts[2].Y = -size; verts[2].Z = 0.0f;
               verts[3].X = size; verts[3].Y = size; verts[3].Z = 0.0f;

               /* Back face. */
               verts[4].X = -size; verts[4].Y = size; verts[4].Z = -size;
               verts[5].X = -size; verts[5].Y = -size; verts[5].Z = -size;
               verts[6].X = size; verts[6].Y = -size; verts[6].Z = -size;
               verts[7].X = size; verts[7].Y = size; verts[7].Z = -size;

               /* Top face. */
               verts[8].X = -size; verts[8].Y = size; verts[8].Z = -size;
               verts[9].X = -size; verts[9].Y = size; verts[9].Z = size;
               verts[10].X = size; verts[10].Y = size; verts[10].Z = size;
               verts[11].X = size; verts[11].Y = size; verts[11].Z = -size;

               /* Bottom face. */
               verts[12].X = -size; verts[12].Y = -size; verts[12].Z = -size;
               verts[13].X = -size; verts[13].Y = -size; verts[13].Z = size;
               verts[14].X = size; verts[14].Y = -size; verts[14].Z = size;
               verts[15].X = size; verts[15].Y = -size; verts[15].Z = -size;

               /* Left face. */
               verts[16].X = -size; verts[16].Y = size; verts[16].Z = -size;
               verts[17].X = -size; verts[17].Y = -size; verts[17].Z = -size;
               verts[18].X = -size; verts[18].Y = -size; verts[18].Z = size;
               verts[19].X = -size; verts[19].Y = size; verts[19].Z = size;

               /* Right face. */
               verts[20].X = size; verts[20].Y = size; verts[20].Z = -size;
               verts[21].X = size; verts[21].Y = -size; verts[21].Z = -size;
               verts[22].X = size; verts[22].Y = -size; verts[22].Z = size;
               verts[23].X = size; verts[23].Y = size; verts[23].Z = size;

               /* Allocate a buffer for our array. */
               glGenBuffers(1, &vboID);
               /* Set our buffer to active. */
               glBindBuffer(GL_ARRAY_BUFFER, vboID);
               /* Upload our array to the active buffer. */
               glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
          }

          /******************************************************************************
           Cube::~Cube();
                    Arguments:
                         void

                    Description:
                         Destruct the cube and free any indebted resources.

                    Returns:
                         void
          *******************************************************************************/
          Cube::~Cube()
          {
               if(glIsBuffer(vboID))
                    glDeleteBuffers(1, &vboID);
          }

          /******************************************************************************
           GLvoid Cube::Cube();
                    Arguments:
                         void

                    Description:
                         Renders the Cube to the back buffer.

                    Returns:
                         void
                         *******************************************************************************/
          GLvoid Cube::Draw()
          {
               /* Set the vertex attribute to active. */
               glEnableVertexAttribArray(Attribute_Position);

               /* Enable our buffer. */
               glBindBuffer(GL_ARRAY_BUFFER, vboID);

               /* Tell OpenGL that we're using floats as vertex positions. */
               glVertexAttribPointer(Attribute_Position, 3, GL_FLOAT, GL_FALSE, 0, 0);

               /* Draw our cube! */
               glDrawArrays(GL_QUADS, 0, 24);

               /* Disable the vertex attributes before we go. */
               glDisableVertexAttribArray(Attribute_Position);
          }

     } /* Namespace Common. */
} /* Namespace LOGL_GLib. */
